#include "qcomboboxmodel.h"

QComboBoxModel::QComboBoxModel(QObject *parent)
    : QAbstractListModel{parent}
{
    values = new QList<QPair<int,QString>>();
}

int QComboBoxModel::rowCount(const QModelIndex &) const
{
    return values->count();
}

void QComboBoxModel::populate(QList<QPair<int,QString>> *newValues)
{
    int idx = this->values->count();
    this->beginInsertRows(QModelIndex(), 1, idx);
    this->values = newValues;
    endInsertRows();
 }

QVariant QComboBoxModel::data( const QModelIndex &index, int role ) const
{

    QVariant value;

        switch (role)
        {
            case Qt::DisplayRole: //string
            {
                value = this->values->value(index.row()).second;
            }
            break;

            case Qt::UserRole: //data
            {
                value = this->values->value(index.row()).first;
            }
            break;

            default:
                break;
        }

    return value;
}

void QComboBoxModel::deleteRow(int idx)
{
    this->beginRemoveRows(QModelIndex(), idx,idx);
    (*this->values).removeAt(idx);
    this->endRemoveRows();
}

void QComboBoxModel::clear() {
    beginResetModel();
    endResetModel();
}
